package Janelas.SQL;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.JButton;

import Banco.ConnectionFactory;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class BuscaUser extends JFrame {

	private static final long serialVersionUID = 2L;
	private JPanel contentPane;
	private JTextField buscaUser;
	private JTable table;

	public BuscaUser() {
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setTitle("Busca Usu\u00E1rio - Occurence Safe");
		setResizable(false);
		setBounds(100, 100, 500, 340);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		buscaUser = new JTextField();
		buscaUser.setBounds(56, 80, 221, 20);
		contentPane.add(buscaUser);
		buscaUser.setColumns(10);
		
		JLabel lblParaFazerA = new JLabel("Para fazer a busca, preencha o campo abaixo:");
		lblParaFazerA.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblParaFazerA.setBounds(10, 11, 296, 14);
		contentPane.add(lblParaFazerA);
		
		JLabel lblNome = new JLabel("Nome:");
		lblNome.setBounds(10, 83, 46, 14);
		contentPane.add(lblNome);
		
		JLabel lblOccurenceSafe = new JLabel("Occurence Safe");
		lblOccurenceSafe.setFont(new Font("Trebuchet MS", Font.BOLD | Font.ITALIC, 21));
		lblOccurenceSafe.setBounds(332, 4, 152, 20);
		contentPane.add(lblOccurenceSafe);
		
		table = new JTable();
		table.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"NomeUser", "CPFUser"
			}
		));
		table.setToolTipText("");
		table.setBounds(10, 150, 474, 96);
		contentPane.add(table);
		
		JButton btnBuscar = new JButton("Buscar");
		btnBuscar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Connection con;
				String query;

				query = "SELECT * FROM USUARIO WHERE NOME_USUARIO = ?";
				
				try {
					con = ConnectionFactory.getConnection();
					PreparedStatement cmd = con.prepareStatement(query);
					cmd.setString(1, buscaUser.getText());
					
					ResultSet rs = cmd.executeQuery();
					DefaultTableModel model = (DefaultTableModel) table.getModel();
					
					model.setNumRows(0);
					while(rs.next()){
						model.addRow(new Object[]{rs.getString("NOME_USUARIO"), rs.getString("CPF_USUARIO")});
					}
					
					cmd.executeUpdate();
					cmd.close();
					con.close();

				} catch (SQLException e1) {

					e1.printStackTrace();
				}}
		});
		btnBuscar.setBounds(141, 278, 89, 23);
		contentPane.add(btnBuscar);
		
		JButton btnCancelar = new JButton("Cancelar");
		btnCancelar.setBounds (395, 278, 89, 23);
		contentPane.add(btnCancelar);
		
		JButton btnAtualizar = new JButton("Atualizar");
		btnAtualizar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				CadastroUser tela1 = new CadastroUser();
				tela1.setVisible(true);
			}
		});
		btnAtualizar.setBounds(10, 278, 89, 23);
		contentPane.add(btnAtualizar);
		
		JButton btnExcluir = new JButton("Excluir");
		btnExcluir.setBounds(269, 278, 89, 23);
		contentPane.add(btnExcluir);
	}
}
